<template>
  <div id="order">
    <el-main>
      <el-table :data="order">
        <el-table-column prop="orderNumber" label="订单号" width="190"></el-table-column>
        <el-table-column prop="name" label="昵称" width="70"></el-table-column>
        <el-table-column prop="entryTime" width="200" label="入库时间">
        </el-table-column>
        <el-table-column prop="outTime" label="出库时间" width="200">
          <template slot-scope="scope">
            <span v-if="+scope.row.outTime == ''">
              <el-popconfirm
                  confirm-button-text='好的'
                  cancel-button-text='不用了'
                  icon="el-icon-info"
                  icon-color="red"
                  title="确定要出库吗？"
                  @confirm="outParking(scope.row.id,scope.row.orderState)">
                   <el-button slot="reference">出库</el-button>
</el-popconfirm>

            </span>
            <span v-else-if="+scope.row.outTime !=''">{{ scope.row.outTime }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="parkingSpaceNumber" label="车位" width="120">
        </el-table-column>
        <el-table-column prop="licensePlateNumber" label="车牌号" width="100">
        </el-table-column>
        <el-table-column prop="orderPrice" label="订单金额" width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.orderPrice }}元</span>
          </template>
        </el-table-column>
        <el-table-column prop="orderState" label="支付状态" width="100">
          <template slot-scope="scope">
             <el-popconfirm
                confirm-button-text='好的'
                cancel-button-text='不用了'
                icon="el-icon-info"
                icon-color="red"
                title="确定要支付吗？"
                @confirm="pay(scope.row)" v-if="+scope.row.orderState === 0">
                   <el-button slot="reference">去支付</el-button>
</el-popconfirm>
            <span v-else-if="+scope.row.orderState === 1">已支付</span>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
  </div>
</template>

<script>
export default {
  name: "user_order",
  props: {
    order: {
      type: Array,
      default: () => []
    },
    email: null,
  },
  methods: {
    pay(order) {
      if (order.outTime==null){
        alert("请先出库！")
        console.log("请先出库")
      }else {
        this.$http.get("http://localhost:8088/pay", {
          params: {
            id: order.id,
          }
        }).then(resp => {
          this.$router.push({
            name:'pay',
            query:{
              id:order.id,
              price:order.price,
              pay:resp.data,
              email: this.email,
            }
          })
          console.log(resp.data)
          if (order.orderState == 1) {
            this.$http.get("http://localhost:8088/user/findCompleteOrder", {
              params: {
                email: this.email,
              }
            }).then(resp => {
              this.order = resp.data
              this.$router.push({
                name: 'user_order',
                query: {
                  order: this.order,
                  email: this.email,
                }
              }).catch(err => {
                console.log(err)
              })
            }).catch(err => {
              console.log(err)
            })
          }
        }).catch({})
      }
    },
    outParking(id, orderState) {
      this.$http.get("http://localhost:8088/user/outParking?", {
        params: {
          id: id,
        }
      }).then(resp => {
        console.log(resp.data)
        if (orderState == 0) {
          this.$http.get("http://localhost:8088/user/findNoCompleteOrder", {
            params: {
              email: this.email,
            }
          }).then(resp => {
            this.order = resp.data
            this.$router.push({
              name: 'user_order',
              query: {
                order: this.order,
                email: this.email,
              }
            }).catch(err => {
              console.log(err)
            })
          }).catch(err => {
            console.log(err)
          })
        }
        if (orderState == 1) {
          this.$http.get("http://localhost:8088/user/findCompleteOrder", {
            params: {
              email: this.email,
            }
          }).then(resp => {
            this.order = resp.data
            this.$router.push({
              name: 'user_order',
              query: {
                order: this.order,
                email: this.email,
              }
            }).catch(err => {
              console.log(err)
            })
          }).catch(err => {
            console.log(err)
          })
        }
      }).catch({})
    }
  }
}
</script>

<style scoped>
#order {
  width: 100%;

}

</style>